package cn.itcast.mongodb;

import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import org.bson.Document;

import java.util.function.Consumer;

/**
 * Created by bothin
 * At 2020/3/4 9:51
 * Description: MongoDB的一个简单的连接 查询例子，而CURD的例子在单元测试test包中演示
 */
public class MongoDBDemo {
    public static void main(String[] args) {
        // 建立连接
        MongoClient mongoClient = MongoClients.create("mongodb://118.89.193.107:27017");
        // 选择数据库
        MongoDatabase mongoDatabase = mongoClient.getDatabase("testdb");
        // 选择表
        MongoCollection<Document> userCollection = mongoDatabase.getCollection("user");
        // 查询数据
        userCollection.find().limit(10).forEach((Consumer < ? super Document >)document -> {
            System.out.println(document.toJson());
        });
        /*
        userCollection.find().limit(10).forEach(new Consumer<Document>() {
            @Override
            public void accept(Document document) {
                System.out.println(document.toJson());
            }
        });
        */

        //关闭连接
        mongoClient.close();


    }
}
